package com.linguochao.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author linguochao
 * @Description rabbit延迟队列配置
 * @Date 2020/7/29 15:33
 */
@Configuration
public class RabbitDelayConfig {

    //======================声明Exchange=============================
    public static final String DELAY_EXCHANGE_NAME = "delay.queue.linguochao.exchange";
    public static final String DEAD_LETTER_EXCHANGE = "delay.queue.linguochao.deadletter.exchange";

    // 声明延时Exchange
    @Bean("delayExchange")
    public DirectExchange delayExchange(){
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }

    // 声明死信Exchange
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }
   //======================声明Exchange=============================


    //======================声明死信队列=============================
    public static final String DEAD_LETTER_QUEUE_NAME = "delay.queue.linguochao.deadletter";
    public static final String DEAD_LETTER_QUEUE_ROUTING_KEY = "delay.queue.linguochao.deadletter.routingkey";
    // 声明死信队列 用于接收延时处理的消息
    @Bean("deadLetterQueue")
    public Queue deadLetterQueueA(){
        return new Queue(DEAD_LETTER_QUEUE_NAME);
    }

    // 声明死信队列绑定关系
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueue") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_ROUTING_KEY);
    }
    //======================声明死信队列=============================


   //======================声明1小时延迟队列================================
    public static final String DELAY_QUEUE_ONE_NAME = "delay.queue.linguochao.one";
    public static final String DELAY_QUEUE_ONE_ROUTING_KEY = "delay.queue.linguochao.one.routingkey";
    // 声明延时队
    @Bean("delayQueueOne")
    public Queue delayQueueOne(){
        Map<String, Object> args = new HashMap<>(2);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_ROUTING_KEY);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl", 6*1000);
        return QueueBuilder.durable(DELAY_QUEUE_ONE_NAME).withArguments(args).build();
    }
    // 声明延时队列绑定关系
    @Bean
    public Binding delayBindingOne(@Qualifier("delayQueueOne") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_ONE_ROUTING_KEY);
    }
    //======================声明1小时延迟队列================================
}
